<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;
use App\Engine\Func;

class LogisticsNotes extends DbEloquent{

    protected $table    = 'ygt_logistics_notes';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'logistics_id','title'=>'物流公司','value'=>0,'is_must'=>1],
        ['name'=>'line_id','title'=>'物流专线','value'=>'','is_must'=>1],
        ['name'=>'warehouse_send_id','title'=>'发货单','value'=>0,'is_must'=>1],
        //['name'=>'contact_name','title'=>'客户名','value'=>'','is_must'=>0],
        ['name'=>'province_id','title'=>'客户地址','value'=>'','is_must'=>0],
        ['name'=>'city_id','title'=>'客户地址','value'=>'','is_must'=>0],
        ['name'=>'area_id','title'=>'客户地址','value'=>'','is_must'=>0],
        ['name'=>'address','title'=>'客户地址','value'=>'','is_must'=>0],
        ['name'=>'logistics_sn','title'=>'运单号','value'=>'','is_must'=>1],
        ['name'=>'customer_order_id','title'=>'客户订单','value'=>'','is_must'=>0],
        ['name'=>'number','title'=>'数量','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'packages','title'=>'件数','value'=>0,'is_must'=>0],
        ['name'=>'weight','title'=>'重量','value'=>0,'is_must'=>0],
        ['name'=>'freight','title'=>'运费','value'=>0,'is_must'=>0],
        ['name'=>'finish_time','title'=>'到货时间','value'=>0,'is_must'=>0],
        ['name'=>'is_remind_finish','title'=>'到货提醒','value'=>0,'is_must'=>0],
        ['name'=>'driving_license','title'=>'行驶证','value'=>'','is_must'=>0],
        ['name'=>'driver_license','title'=>'驾驶证','value'=>'','is_must'=>0],
        ['name'=>'img_ids','title'=>'附加图','value'=>'','is_must'=>0],
    ];

    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        $provinceId         = $data['province_id'];
        $cityId             = $data['city_id'];
        $areaId             = $data['area_id'];
        $provinceName       = $cityName = $areaName = '';
        if($provinceId != 0)
        {
            $where          = ['provinceID'=>$provinceId];
            $collection     = Province::where($where)->first();
            $provinceName   = $collection->province;
        }
        if($cityId != 0)
        {
            $where          = ['cityID'=>$cityId];
            $collection     = City::where($where)->first();
            $cityName       = $collection->city;
        }
        if($areaId != 0)
        {
            $where          = ['areaID'=>$areaId];
            $collection     = Area::where($where)->first();
            $areaName       = $collection->area;
        }
        $data['province_name']  = $provinceName;
        $data['city_name']      = $cityName;
        $data['area_name']      = $areaName;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where, $data,$pk);
        } else {
            //$logisticsSnArr = explode(',',$data['logistics_sn']);
            //$data['logistics_sn']   = $val;
            $result     = self::insertOneData($data,$pk);
        }
        return $result;
    }
    //app详细页面字段过滤
    public static function getInfoData($where)
    {
        $fields         = '*';
        $collection     = self::getInfo($where,$fields);
        $info           = [];
        if($collection){
            $info               = $collection->toArray();
            $logisticsId        = $info['logistics_id'];
            $where              = ['id'=>$logisticsId];
            $logisticsList      = Logistics::getLists($where);
            $logisticsName      = $logisticsList[0]['title'];
            $logisticsLogo      = $logisticsList[0]['logo'];
            $info['logistics_name'] = $logisticsName;
            $info['logistics_logo'] = $logisticsLogo;
            $totalMoney         = $info['weight'] * $info['freight'];
            $info['total_money'] = $totalMoney;
            $info               = self::changeInfo($info);
        }
        return $info;
    }
    //数据表里取出数据后的转换
    public static function changeInfo($data)
    {
        //行驶证
        $drivingLicense         = $data['driving_license'];
        $drivingLicenseImg      = Func::getImgUrlById($drivingLicense);
        //驾驶证
        $driverLicense          = $data['driver_license'];
        $driverLicenseImg       = Func::getImgUrlById($driverLicense);
        //附加图
        $imgIds                 = $data['img_ids'];
        $imgImg                 = Func::getImgUrlByIds($imgIds);
        $isRemindFinish         = $data['is_remind_finish'];
        $isRemindFinishTxt      = $isRemindFinish == 1 ? '是' : '否';
        $data['driving_license_img']    = $drivingLicenseImg;
        $data['driver_license_img']     = $driverLicenseImg;
        $data['img_img']                 = $imgImg;
        $data['is_remind_finish']       = $isRemindFinishTxt;
        $data['status_name']                 = '发货中';
        unset($data['driving_license']);
        unset($data['driver_license']);
        unset($data['img_ids']);
        return $data;
    }
    //数据表里取出数据后的转换
    //$type 1取一条 2取全部
    public static function getCustomerOrder($customerOrderId,$type=2)
    {
        $defaultImg     = env('APP_URL').'/assets/mobile/common/images/order_list_default.png';
        $resultArr      = [
            'order_title'=>'非订单发货', 'order_type_title'=>'',
            'customer_name'=>'', 'diagram_img'=>$defaultImg,
        ];
        if($customerOrderId){
            $orderList      = [];
            $idArr          = explode(',',$customerOrderId);
            $lists          = CustomerOrder::whereIn('id',$idArr)->get()->toArray();
            foreach($lists as $key=>$val)
            {
                $productionCaseDiagram  = $val['production_case_diagram'];
                $diagramArr             = explode(',',$productionCaseDiagram);
                $imgId                  = isset($diagramArr[0]) ? $diagramArr[0] : 0;
                $diagramImg             = $defaultImg;
                if($imgId)
                {
                    $imgUrl             = Func::getImgUrlById($imgId);
                    $diagramImg         = $imgUrl ? $imgUrl : $diagramImg;
                }
                $orderTypeTitle         = OrderType::getOneValueById($val['order_type'], 'title');
                $customerName           = \App\Engine\Process::changeFieldName23($val['field_name_23']);;
                $arr                    = [
                    'order_title'=>$val['order_title'],
                    'order_type_title'=>$orderTypeTitle,
                    'customer_name'=>$customerName,
                    'diagram_img'=>$diagramImg,
                ];
                $orderList[]            = $arr;
            }
            if($type == 1 ){
                if(!empty($orderList)){
                    $result     = $orderList[0];
                }else{
                    $result     = [
                        'order_title'=>'', 'order_type_title'=>'',
                        'customer_name'=>'', 'diagram_img'=>$defaultImg,
                    ];
                }
            }else{
                $result     = $orderList;
            }
        }else{
            if($type == 2 ){
                $result     = [];
            }else{
                $result     = $resultArr;
            }
        }
        return $result;
    }

}

